
#include "Allegro_initialize.h"
#include "game.h"
using namespace std;


int main(void)
{
	//------------------------------------------
	//Initialise the Resource Stack
	//------------------------------------------
	
	Allegro_resources resources = {NULL, NULL, NULL};

	SETUP_STATUS  status = init_alleg(resources);
	string error_message = create_error_message(status);
	
	if (status != SETUP_SUCCESFULL)
	{
		display_setup_error_message(error_message);
	}
	//------------------------------------------

	//------------------------------------------
	//Declare Local Variables
	//------------------------------------------
	//Initiate the Game Object (Contains all other objects!)
	Game thisgame;


	
	//Set up the user input keys
	bool arrow_keys[4] = {false, false, false, false};
	bool shoot_key = false; 
	Direction dir = NONE;

	//Define loop and rendering booleans
	bool done = false;
	bool redraw = true;

	//------------------------------------------
	//Enter the Game loop
	//------------------------------------------
	al_start_timer(resources._timer);
	while(!done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(resources._event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					arrow_keys[UP] = true;
					dir = UP;
					break;
				case ALLEGRO_KEY_DOWN:
					arrow_keys[DOWN] = true;
					dir = DOWN;
					break;
				case ALLEGRO_KEY_RIGHT:
					arrow_keys[RIGHT] = true;
					dir = RIGHT;
					break;
				case ALLEGRO_KEY_LEFT:
					arrow_keys[LEFT] = true;
					dir = LEFT;
					break;
				case ALLEGRO_KEY_SPACE:
					shoot_key = true;
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			dir = NONE;
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					arrow_keys[UP] = false;
					break;
				case ALLEGRO_KEY_DOWN:
					arrow_keys[DOWN] = false;
					break;
				case ALLEGRO_KEY_RIGHT:
					arrow_keys[RIGHT] = false;
					break;
				case ALLEGRO_KEY_LEFT:
					arrow_keys[LEFT] = false;
					break;
				case ALLEGRO_KEY_SPACE:
					shoot_key = false;
					break;
				case ALLEGRO_KEY_ESCAPE:
					done = true;
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			thisgame.update(dir);
			redraw = true;
		}

		if(redraw && al_is_event_queue_empty(resources._event_queue))
		{
			redraw = false;
			int pos_x = thisgame._dig_dug.get_x_location();
			int pos_y = thisgame._dig_dug.get_y_location();
			al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(255,0,255));
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		}
	}


	al_destroy_event_queue(resources._event_queue);
	//if (resources._event_queue)
	//{
	//	string message("COULD NOT DESTROY EVENT QUEUE");
	//	display_setup_error_message(message);
	//}


	al_destroy_timer(resources._timer);
	al_destroy_display(resources._display);
	return 0;
}

